﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SiteManager.Areas.Manager.Models;
using SiteManager.Models;
using SiteManager.Core.Security;
using SiteManager.Core;
using SiteManager.Core.Filters;
using System.Data;
using SiteManager.Core.Helper;

namespace SiteManager.Areas.Manager.Controllers
{
    public class AccountController : Controller
    {
        SiteManagerEntities db = Factorys.Instance.GetEntity();

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Index(AdminLoginModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Username))
            {
                ViewBag.Error = "用户名不能为空";
                return View();
            }
            if (string.IsNullOrEmpty(model.Password))
            {
                ViewBag.Error = "密码不能为空";
                return View();
            }
            string password = Encryption.Format(model.Password, EncryptType.MD5);
            
            admins admin = db
                .admins.Where(a => 
                    a.Username == model.Username &&
                    a.Password == password
                ).SingleOrDefault();

            if (admin == null || admin.AdminID < 0)
            {
                // Utils.Instance.MsgError("用户名与密码不匹配");
                return View();
            }

            // 到此表示登陆成功
            Utils.SetAdmin(admin);
            return RedirectToAction("Index", "Admin");
        }

        public ActionResult Exit()
        {
            Utils.SetAdmin(null);
            return RedirectToAction("Index");
        }

        [SiteAdminFilter]
        public ActionResult PasswordChange()
        {
            return View();
        }

        [SiteAdminFilter]
        [HttpPost]
        public ActionResult PasswordChange(int oldPassword, string newPassword, string reNewPassword)
        {
            if (oldPassword.ToString() == "" || newPassword == "" || reNewPassword == "")
            {
                ViewBag.Error = "请将信息填写完整";
                return View();
            }
            // 比较新密码与重复的新密码是否完全吻合
            if (newPassword.Equals(reNewPassword))
            {
                if (!oldPassword.Equals(newPassword))
                {
                    admins admin = Utils.GetAdmin();
                    //oldPassword = Encryption.Format(oldPassword, EncryptType.MD5);
                    newPassword = Encryption.Format(newPassword, EncryptType.MD5);
                    if (admin.Password.Equals(oldPassword))
                    {
                        admin.Password = newPassword;

                        db.admins.Attach(admin);
                        db.ObjectStateManager.ChangeObjectState(admin, EntityState.Modified);
                        db.SaveChanges();
                        Utils.SetAdmin(admin);
                        ViewBag.Success = "密码修改成功";
                        return View();
                    }
                    else
                    {
                        ViewBag.Error = "旧密码输入错误";
                    }
                }
                else
                {
                    ViewBag.Error = "旧密码与新密码不能相同";
                }
            }
            else
            {
                ViewBag.Error = "新密码与重复密码不匹配";
            }
            return View();
        }

        [SiteAdminFilter]
        public ActionResult UserInfo()
        {
            return View();
        }

        [SiteAdminFilter]
        public ActionResult AvatarManager()
        {
            return View();
        }
    }
}